package language.monkey.token

class Token(val type: TokenType, var literal: String) {


    override fun toString(): String {
        return "Token($type, '$literal')"
    }

    companion object {

        val keywords = mapOf(
            "fn" to TokenType.FUNCTION,
            "let" to TokenType.LET,
            "true" to TokenType.TRUE,
            "false" to TokenType.FALSE,
            "if" to TokenType.IF,
            "else" to TokenType.ELSE,
            "return" to TokenType.RETURN
        )

        /**
         * 获取关键字对应的TokenType
         *
         * 非关键字返回IDENT(变量)
         */
        fun lookupIdent(ident: String): TokenType {
            return keywords[ident] ?: TokenType.IDENT
        }

        fun newToken(tokenType: TokenType, ch: Char): Token {
            return Token(tokenType, ch.toString())
        }

        fun newToken(tokenType: TokenType, literal: String): Token {
            return Token(tokenType, literal)
        }

        fun newByLiteral(literal: String): Token {
            return Token(
                lookupIdent(literal), literal
            )
        }
    }
}

